﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 事件监听器
/// </summary>
public class VIPEventListener
{

    private static Dictionary<VIPEventCode, Delegate> m_EventTable = new Dictionary<VIPEventCode, Delegate>();

    private static void OnListenerAdding(VIPEventCode eventCode, Delegate callBack)
    {
        if (!m_EventTable.ContainsKey(eventCode))
        {
            m_EventTable.Add(eventCode, null);
        }
        Delegate d = m_EventTable[eventCode];
        if (d != null && d.GetType() != callBack.GetType())
        {
            throw new Exception(string.Format("尝试为事件{0}添加不同类型的委托，当前事件所对应的委托是{1}，要添加的委托类型为{2}", eventCode, d.GetType(), callBack.GetType()));
        }
    }

    private static void OnListenerRemoving(VIPEventCode eventCode, Delegate callBack)
    {
        if (m_EventTable.ContainsKey(eventCode))
        {
            Delegate d = m_EventTable[eventCode];
            if (d == null)
            {
                throw new Exception(string.Format("移除监听错误：事件{0}没有对应的委托", eventCode));
            }
            else if (d.GetType() != callBack.GetType())
            {
                throw new Exception(string.Format("移除监听错误：尝试为事件{0}移除不同类型的委托，当前委托类型为{1}，要移除的委托类型为{2}", eventCode, d.GetType(), callBack.GetType()));
            }
        }
        else
        {
            throw new Exception(string.Format("移除监听错误：没有事件码{0}", eventCode));
        }
    }
    private static void OnListenerRemoved(VIPEventCode eventCode)
    {
        if (m_EventTable[eventCode] == null)
        {
            m_EventTable.Remove(eventCode);
        }
    }

    /// <summary>
    /// 无参数
    /// </summary>
    /// <param name="eventCode"></param>
    /// <param name="callBack"></param>
    public static void AddListener(VIPEventCode eventCode, VIPCallBack callBack)
    {
        OnListenerAdding(eventCode, callBack);
        m_EventTable[eventCode] = (VIPCallBack)m_EventTable[eventCode] + callBack;
    }

    /// <summary>
    /// 一个参数
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="eventCode"></param>
    /// <param name="callBack"></param>
    public static void AddListener<T>(VIPEventCode eventCode, VIPCallBack<T> callBack)
    {
        OnListenerAdding(eventCode, callBack);
        m_EventTable[eventCode] = (VIPCallBack<T>)m_EventTable[eventCode] + callBack;
    }

    /// <summary>
    /// 二个参数
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <typeparam name="X"></typeparam>
    /// <param name="eventCode"></param>
    /// <param name="callBack"></param>
    public static void AddListener<T, X>(VIPEventCode eventCode, VIPCallBack<T, X> callBack)
    {
        OnListenerAdding(eventCode, callBack);
        m_EventTable[eventCode] = (VIPCallBack<T, X>)m_EventTable[eventCode] + callBack;
    }

    /// <summary>
    /// 三个参数
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <typeparam name="X"></typeparam>
    /// <typeparam name="Y"></typeparam>
    /// <param name="eventCode"></param>
    /// <param name="callBack"></param>
    public static void AddListener<T, X, Y>(VIPEventCode eventCode, VIPCallBack<T, X, Y> callBack)
    {
        OnListenerAdding(eventCode, callBack);
        m_EventTable[eventCode] = (VIPCallBack<T, X, Y>)m_EventTable[eventCode] + callBack;
    }

    /// <summary>
    /// 四个参数
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <typeparam name="X"></typeparam>
    /// <typeparam name="Y"></typeparam>
    /// <typeparam name="Z"></typeparam>
    /// <param name="eventCode"></param>
    /// <param name="callBack"></param>
    public static void AddListener<T, X, Y, Z>(VIPEventCode eventCode, VIPCallBack<T, X, Y, Z> callBack)
    {
        OnListenerAdding(eventCode, callBack);
        m_EventTable[eventCode] = (VIPCallBack<T, X, Y, Z>)m_EventTable[eventCode] + callBack;
    }

    /// <summary>
    /// 五个参数
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <typeparam name="X"></typeparam>
    /// <typeparam name="Y"></typeparam>
    /// <typeparam name="Z"></typeparam>
    /// <typeparam name="W"></typeparam>
    /// <param name="eventCode"></param>
    /// <param name="callBack"></param>
    public static void AddListener<T, X, Y, Z, W>(VIPEventCode eventCode, VIPCallBack<T, X, Y, Z, W> callBack)
    {
        OnListenerAdding(eventCode, callBack);
        m_EventTable[eventCode] = (VIPCallBack<T, X, Y, Z, W>)m_EventTable[eventCode] + callBack;
    }

    /// <summary>
    /// 无参数
    /// </summary>
    /// <param name="eventCode"></param>
    /// <param name="callBack"></param>
    public static void RemoveListener(VIPEventCode eventCode, VIPCallBack callBack)
    {
        OnListenerRemoving(eventCode, callBack);
        m_EventTable[eventCode] = (VIPCallBack)m_EventTable[eventCode] - callBack;
        OnListenerRemoved(eventCode);
    }

    /// <summary>
    /// 一个参数
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="eventCode"></param>
    /// <param name="callBack"></param>
    public static void RemoveListener<T>(VIPEventCode eventCode, VIPCallBack<T> callBack)
    {
        OnListenerRemoving(eventCode, callBack);
        m_EventTable[eventCode] = (VIPCallBack<T>)m_EventTable[eventCode] - callBack;
        OnListenerRemoved(eventCode);
    }

    /// <summary>
    /// 二个参数
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <typeparam name="X"></typeparam>
    /// <param name="eventCode"></param>
    /// <param name="callBack"></param>
    public static void RemoveListener<T, X>(VIPEventCode eventCode, VIPCallBack<T, X> callBack)
    {
        OnListenerRemoving(eventCode, callBack);
        m_EventTable[eventCode] = (VIPCallBack<T, X>)m_EventTable[eventCode] - callBack;
        OnListenerRemoved(eventCode);
    }

    /// <summary>
    /// 三个参数
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <typeparam name="X"></typeparam>
    /// <typeparam name="Y"></typeparam>
    /// <param name="eventCode"></param>
    /// <param name="callBack"></param>
    public static void RemoveListener<T, X, Y>(VIPEventCode eventCode, VIPCallBack<T, X, Y> callBack)
    {
        OnListenerRemoving(eventCode, callBack);
        m_EventTable[eventCode] = (VIPCallBack<T, X, Y>)m_EventTable[eventCode] - callBack;
        OnListenerRemoved(eventCode);
    }

    /// <summary>
    /// 四个参数
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <typeparam name="X"></typeparam>
    /// <typeparam name="Y"></typeparam>
    /// <typeparam name="Z"></typeparam>
    /// <param name="eventCode"></param>
    /// <param name="callBack"></param>
    public static void RemoveListener<T, X, Y, Z>(VIPEventCode eventCode, VIPCallBack<T, X, Y, Z> callBack)
    {
        OnListenerRemoving(eventCode, callBack);
        m_EventTable[eventCode] = (VIPCallBack<T, X, Y, Z>)m_EventTable[eventCode] - callBack;
        OnListenerRemoved(eventCode);
    }

    /// <summary>
    /// 五个参数
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <typeparam name="X"></typeparam>
    /// <typeparam name="Y"></typeparam>
    /// <typeparam name="Z"></typeparam>
    /// <typeparam name="W"></typeparam>
    /// <param name="eventCode"></param>
    /// <param name="callBack"></param>
    public static void RemoveListener<T, X, Y, Z, W>(VIPEventCode eventCode, VIPCallBack<T, X, Y, Z, W> callBack)
    {
        OnListenerRemoving(eventCode, callBack);
        m_EventTable[eventCode] = (VIPCallBack<T, X, Y, Z, W>)m_EventTable[eventCode] - callBack;
        OnListenerRemoved(eventCode);
    }


    /// <summary>
    /// 无参数
    /// </summary>
    /// <param name="eventCode"></param>
    public static void TriggleListener(VIPEventCode eventCode)
    {
        Delegate d;
        if (m_EventTable.TryGetValue(eventCode, out d))
        {
            VIPCallBack callBack = d as VIPCallBack;
            if (callBack != null)
            {
                callBack();
            }
            else
            {
                throw new Exception(string.Format("广播事件错误：事件{0}对应委托具有不同的类型", eventCode));
            }
        }
    }

    /// <summary>
    /// 一个参数
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="eventCode"></param>
    /// <param name="arg"></param>
    public static void TriggleListener<T>(VIPEventCode eventCode, T arg)
    {
        Delegate d;
        if (m_EventTable.TryGetValue(eventCode, out d))
        {
            VIPCallBack<T> callBack = d as VIPCallBack<T>;
            if (callBack != null)
            {
                callBack(arg);
            }
            else
            {
                throw new Exception(string.Format("广播事件错误：事件{0}对应委托具有不同的类型", eventCode));
            }
        }
    }

    /// <summary>
    /// 二个参数
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <typeparam name="X"></typeparam>
    /// <param name="eventCode"></param>
    /// <param name="arg1"></param>
    /// <param name="arg2"></param>
    public static void TriggleListener<T, X>(VIPEventCode eventCode, T arg1, X arg2)
    {
        Delegate d;
        if (m_EventTable.TryGetValue(eventCode, out d))
        {
            VIPCallBack<T, X> callBack = d as VIPCallBack<T, X>;
            if (callBack != null)
            {
                callBack(arg1, arg2);
            }
            else
            {
                throw new Exception(string.Format("广播事件错误：事件{0}对应委托具有不同的类型", eventCode));
            }
        }
    }

    /// <summary>
    /// 三个参数
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <typeparam name="X"></typeparam>
    /// <typeparam name="Y"></typeparam>
    /// <param name="eventCode"></param>
    /// <param name="arg1"></param>
    /// <param name="arg2"></param>
    /// <param name="arg3"></param>
    public static void TriggleListener<T, X, Y>(VIPEventCode eventCode, T arg1, X arg2, Y arg3)
    {
        Delegate d;
        if (m_EventTable.TryGetValue(eventCode, out d))
        {
            VIPCallBack<T, X, Y> callBack = d as VIPCallBack<T, X, Y>;
            if (callBack != null)
            {
                callBack(arg1, arg2, arg3);
            }
            else
            {
                throw new Exception(string.Format("广播事件错误：事件{0}对应委托具有不同的类型", eventCode));
            }
        }
    }

    /// <summary>
    /// 四个参数
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <typeparam name="X"></typeparam>
    /// <typeparam name="Y"></typeparam>
    /// <typeparam name="Z"></typeparam>
    /// <param name="eventCode"></param>
    /// <param name="arg1"></param>
    /// <param name="arg2"></param>
    /// <param name="arg3"></param>
    /// <param name="arg4"></param>
    public static void TriggleListener<T, X, Y, Z>(VIPEventCode eventCode, T arg1, X arg2, Y arg3, Z arg4)
    {
        Delegate d;
        if (m_EventTable.TryGetValue(eventCode, out d))
        {
            VIPCallBack<T, X, Y, Z> callBack = d as VIPCallBack<T, X, Y, Z>;
            if (callBack != null)
            {
                callBack(arg1, arg2, arg3, arg4);
            }
            else
            {
                throw new Exception(string.Format("广播事件错误：事件{0}对应委托具有不同的类型", eventCode));
            }
        }
    }
    /// <summary>
    /// 五个参数
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <typeparam name="X"></typeparam>
    /// <typeparam name="Y"></typeparam>
    /// <typeparam name="Z"></typeparam>
    /// <typeparam name="W"></typeparam>
    /// <param name="eventCode"></param>
    /// <param name="arg1"></param>
    /// <param name="arg2"></param>
    /// <param name="arg3"></param>
    /// <param name="arg4"></param>
    /// <param name="arg5"></param>
    public static void TriggleListener<T, X, Y, Z, W>(VIPEventCode eventCode, T arg1, X arg2, Y arg3, Z arg4, W arg5)
    {
        Delegate d;
        if (m_EventTable.TryGetValue(eventCode, out d))
        {
            VIPCallBack<T, X, Y, Z, W> callBack = d as VIPCallBack<T, X, Y, Z, W>;
            if (callBack != null)
            {
                callBack(arg1, arg2, arg3, arg4, arg5);
            }
            else
            {
                throw new Exception(string.Format("广播事件错误：事件{0}对应委托具有不同的类型", eventCode));
            }
        }
    }

}
